layui.use('table', function () {
    var table = layui.table;
    table.render({
        elem: '#product_size'
        , url: 'size_pageList'
        , toolbar: '#toolbarDemo'
        , title: '产品尺寸表'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', title: 'ID', hide: true}
            , {field: 'autonumber', title: '序号', fixed: 'left', type: 'numbers', width: 50}
            , {field: 'sizeName', title: '名称', width: 100, align: 'center'}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 80}
        ]]
        , page: {theme: '#1E9FFF'}
        , defaultToolbar: false/*['print', 'exports']*/
    });

    //头工具栏事件
    table.on('toolbar(size)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        var data = checkStatus.data;
        switch (obj.event) {
            case 'add' :
                layer.open({
                    type: 1,
                    skin: 'layui-layer-rim',
                    area: ['420px', '200px'], //宽高
                    title: '添加产品尺寸',
                    content: $('.getSizeAdd'),
                    btn: ['保存', '取消'],
                    btn1: function (index, layero) {
                        var sizeName = $('input[name=sizeName]').val().trim();
                        if (sizeName) {
                            $.ajax({
                                url: "size_save",
                                dataType: "json",
                                type: "post",
                                data: {sizeName: sizeName},
                                success: function (data) {
                                    if (data.status) {
                                        table.reload('product_size', {
                                            page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        }, 'data');
                                        layer.msg(data.msg, {icon: 1});
                                    } else {
                                        layer.msg(data.msg, {icon: 2});
                                    }
                                    layer.close(index);
                                }
                            });
                        }
                    },
                    btn2: function (index, layero) {
                        layer.close(index);
                    }

                });
                break;
            case 'delete':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else {
                    var id = '';
                    for (var i = 0; i < data.length; i++) {
                        id += "'" + data[i].id + "',";
                    }
                    $.ajax({
                        url: '${rootPath}/size_delete',
                        data: {ids: id.substr(0, id.length - 1)},
                        dataType: 'json',
                        type: 'post',
                        success: function () {
                            table.reload('product_size');
                        }
                    });

                }
                break;
        }
    });

    //监听行工具事件
    table.on('tool(size)', function (obj) {
        var data = obj.data;
        //console.log(obj)
        if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                $.ajax({
                    url: '${rootPath}/size_delete',
                    data: data,
                    dataType: 'json',
                    type: 'post',
                    success: function (data) {
                        console.log(data)
                    }
                });
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            layer.prompt({
                formType: 2
                , value: data.email
            }, function (value, index) {
                obj.update({
                    email: value
                });
                layer.close(index);
            });
        }
    });

    //查询
    var $ = layui.$, active = {
        reload: function () {
            var keyword = $('input[name=keyword]').val();
            //执行重载
            table.reload('product_size', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {sizeName: keyword}
            }, 'data');
        }
    };

    $('#search').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            $("#search").trigger('click');
        }
    });
});
